package com.itcam.tingshu.account.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itcam.tingshu.account.service.UserAccountService;
import com.itcam.tingshu.common.login.TsLogin;
import com.itcam.tingshu.common.result.Result;
import com.itcam.tingshu.common.util.AuthContextHolder;
import com.itcam.tingshu.model.account.UserAccount;
import com.itcam.tingshu.model.account.UserAccountDetail;
import com.itcam.tingshu.vo.account.AccountLockResultVo;
import com.itcam.tingshu.vo.account.AccountLockVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "用户账户管理")
@RestController
@RequestMapping("api/account/userAccount")
@SuppressWarnings({"unchecked", "rawtypes"})
public class UserAccountApiController {

    @Autowired
    private UserAccountService userAccountService;

    /**
     * 获取账户余额
     * @return
     */
    @TsLogin
    @Operation(summary = "获取账户余额")
    @GetMapping("/getAvailableAmount")
    public Result getAvailableAmount() {
        // 获取用户id
        Long userId = AuthContextHolder.getUserId();
        // 调用服务层方法 —— 根据主键Id查询数据
        UserAccount userAccount = userAccountService.getOne(new LambdaQueryWrapper<UserAccount>().eq(UserAccount::getUserId, userId));
        // 解决空指针异常
        if (null == userAccount) return Result.fail();
        // 返回数据
        return Result.ok(userAccount.getAvailableAmount());
    }

    /**
     * 检查锁定账户金额
     * @param accountLockVo
     * @return
     */
    @TsLogin
    @Operation(summary = "检查与锁定账户金额")
    @PostMapping("/checkAndLock")
    public Result<AccountLockResultVo> checkAndLock(@RequestBody AccountLockVo accountLockVo) {
        // 调用服务层方法
        AccountLockResultVo accountLockResultVo = this.userAccountService.checkAndLock(accountLockVo);
        // 返回数据
        return Result.ok(accountLockResultVo);
    }

    /**
     * 获取充值记录
     * @param page
     * @param limit
     * @return
     */
    @TsLogin
    @Operation(summary = "获取充值记录")
    @GetMapping("/findUserRechargePage/{page}/{limit}")
    public Result findUserRechargePage(@PathVariable Long page, @PathVariable long limit) {
        // 获取用户Id
        Long userId = AuthContextHolder.getUserId();
        // 创建Page对象
        Page<UserAccountDetail> detailPage = new Page<>(page, limit);
        // 调用服务层方法
        IPage<UserAccountDetail> userAccountDetailIPage = userAccountService.findUserRechargePage(detailPage, userId);
        // 返回数据
        return Result.ok(userAccountDetailIPage);
    }

    /**
     * 获取消费记录
     * @param page
     * @param limit
     * @return
     */
    @TsLogin
    @Operation(summary = "获取消费记录")
    @GetMapping("/findUserConsumePage/{page}/{limit}")
    public Result findUserConsumePage(@PathVariable Long page, @PathVariable Long limit) {
        // 获取用户Id
        Long userId = AuthContextHolder.getUserId();
        // 创建Page对象
        Page<UserAccountDetail> detailPage = new Page<>(page, limit);
        // 调用服务层方法
        IPage<UserAccountDetail> userAccountDetailIPage = userAccountService.findUserConsumePage(detailPage, userId);
        // 返回数据
        return null;
    }

}














